package com.design.marketPrj.application.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.design.marketPrj.application.bean.PO.User;
import com.design.marketPrj.application.controller.BaseController;
import com.design.marketPrj.application.service.UserService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.security.util.Password;

@Controller
public class UserIndexController extends BaseController {
    @Autowired
    UserService userService;


    @RequestMapping("/userIndex")
    public String userIndex(Model model){
        model.addAttribute("user",getUser());
        return "user/UserIndex";
    }

    @RequestMapping("/changePassword")
    @ResponseBody
    public JSONObject changePassword(String oldPassword,String newPassword){
        JSONObject jsonObject = new JSONObject();
        User user = getUser();
        //旧密码验证
        ByteSource salt = ByteSource.Util.bytes(getUserName());
        String verifyPassword = new SimpleHash("MD5",oldPassword,salt,1024).toHex();
        if(verifyPassword.equals(user.getPassword()))
        {
            String updatePassword = new SimpleHash("MD5",newPassword,salt,1024).toHex();
            user.setPassword(updatePassword);
            userService.update(user);
            jsonObject.put("code",200);
            jsonObject.put("msg","密码修改成功!");
        }
        else
        {
            jsonObject.put("code",500);
            jsonObject.put("msg","旧密码错误!");
        }
        return jsonObject;
    }

}
